package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.system.dto.request.ImageVerifyCodeRequest;
import com.ssy.lingxi.system.dto.response.ImageVerifyCodeResponse;
import com.ssy.lingxi.system.service.ImageVerifyCodeService;
import com.ssy.lingxi.system.utils.ImageUtil;
import org.apache.commons.codec.digest.Md5Crypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.validation.Valid;

/**
* 图片验证码业务处理类
* @author wrc
* @date 2020/5/21
*/
@Service
public class ImageVerifyCodeServiceImpl implements ImageVerifyCodeService {

    private static Logger logger = LoggerFactory.getLogger(ImageVerifyCodeServiceImpl.class);
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    private ImageVerifyCodeRequest request;


    @Override
    public Wrapper<ImageVerifyCodeResponse> getImageVerifyCode(@Valid ImageVerifyCodeRequest request) {
        //TODO 随机获取图片链接
        //读取网络图片
        ImageVerifyCodeResponse image = ImageUtil.createImage("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1906469856,4113625838&fm=26&gp=0.jpg");
        //  X坐标保存在缓存中
        String md5Str = Md5Crypt.apr1Crypt(request.getAccount());
        stringRedisTemplate.opsForValue().set(md5Str,image.getWidthRandom()+"");
        return Wrapper.success(image);
    }
}
